package com.qf.business.redevnlopes.core.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.qf.data.redenvlopes.dto.RedEnvelopesDto;
import com.qf.data.redenvlopes.entity.RedEnvelopes;
import org.apache.ibatis.annotations.Select;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
 * 红包表(RedEnvelopes)表数据库访问层
 *
 * @author makejava
 * @since 2022-09-27 14:35:25
 */
public interface RedEnvelopesDao extends BaseMapper<RedEnvelopes> {

    /**
     * 批量新增数据（MyBatis原生foreach方法）
     *
     * @param entities List<RedEnvelopes> 实例对象列表
     * @return 影响行数
     */
    int insertBatch(@Param("entities") List<RedEnvelopes> entities);

    /**
     * 批量新增或按主键更新数据（MyBatis原生foreach方法）
     *
     * @param entities List<RedEnvelopes> 实例对象列表
     * @return 影响行数
     * @throws org.springframework.jdbc.BadSqlGrammarException 入参是空List的时候会抛SQL语句错误的异常，请自行校验入参
     */
    int insertOrUpdateBatch(@Param("entities") List<RedEnvelopes> entities);

    @Select("select r.*, (r.scount - ifnull(sum(rd.score), 0)) as hasScore, (r.number - count(rd.id)) as hasCount from " +
            "red_envelopes r left join red_envelopes_details rd on r.id = rd.redid " +
            "where r.id = #{redid}")
    RedEnvelopesDto queryRedById(Integer redid);
}